#Region "Imports"
Imports System.Collections.Generic
#End Region
''' <summary>
''' A groupe of useful functions
''' </summary>
''' <remarks></remarks>
Friend Module util
    ''' <summary>
    ''' Get a PDF array object stored directly in the object or referred to by an indirect reference
    ''' </summary>
    ''' <param name="obj">The source object</param>
    ''' <param name="pdf">The pdf object</param>
    ''' <returns>The list of pdf objects in the array</returns>
    ''' <remarks></remarks>
    Public Function GetPdfArrayFromDirectOrIndirectReference(ByVal obj As clsPdfBaseObject, ByRef pdf As ClsPDF) As List(Of clsPdfBaseObject)
        GetPdfArrayFromDirectOrIndirectReference = Nothing
        Select Case obj.Type
            Case PdfObjectTypes.ArrayObject
                'Array is stored directly in the object
                GetPdfArrayFromDirectOrIndirectReference = obj.GetArray
            Case PdfObjectTypes.IndirectReferenceObject
                'This is an indirect reference to the array
                obj = pdf.GetPdfObjectFromIndirectReferenceObject(obj)
                obj.ParseChildObjects()
                If obj.ChildsCount = 1 Then
                    GetPdfArrayFromDirectOrIndirectReference = obj.child(0).GetArray
                End If
            Case Else
                GetPdfArrayFromDirectOrIndirectReference = Nothing
        End Select
    End Function
    ''' <summary>
    ''' Get a PDF dictionnary object stored directly in the object or referred to by an indirect reference
    ''' </summary>
    ''' <param name="obj">The source object</param>
    ''' <param name="pdf">The pdf object</param>
    ''' <returns>A PDF dictionnary object</returns>
    ''' <remarks></remarks>
    Public Function GetPdfDictionnaryFromDirectOrIndirectReference(ByVal obj As clsPdfBaseObject, ByRef pdf As ClsPDF) As clsPdfDictionnary
        GetPdfDictionnaryFromDirectOrIndirectReference = Nothing
        Select Case obj.Type
            Case PdfObjectTypes.IndirectReferenceObject
                'We have an indirect reference to the ressource dictionnary
                obj = pdf.GetPdfObjectFromIndirectReferenceObject(obj)
                obj.ParseChildObjects()
                GetPdfDictionnaryFromDirectOrIndirectReference = obj.child(0).GetPdfDictionnary
            Case PdfObjectTypes.DictionaryObject
                GetPdfDictionnaryFromDirectOrIndirectReference = obj.GetPdfDictionnary
            Case Else
                GetPdfDictionnaryFromDirectOrIndirectReference = Nothing
        End Select
    End Function
    Public Function CreatePdfDate(ByVal d As Date) As String
        CreatePdfDate = d.ToString(PdfFormatDate)
        CreatePdfDate += "-00'00'"
        CreatePdfDate = "D:" + CreatePdfDate
    End Function
    Public Function PdfDateToString(ByVal pdfDate As String) As String
        If pdfDate.StartsWith(PDF_DATE_IDENTIFIER) Then
            'It is a date that follows PDF date standard
            Try
                pdfDate = pdfDate.Replace(PDF_DATE_IDENTIFIER, "")
                Dim year As Integer = CInt(pdfDate.Substring(0, 4))
                Dim month As Integer = CInt(pdfDate.Substring(4, 2))
                Dim day As Integer = CInt(pdfDate.Substring(6, 2))
                Dim hour As Integer = CInt(pdfDate.Substring(8, 2))
                Dim minute As Integer = CInt(pdfDate.Substring(10, 2))
                Dim second As Integer = CInt(pdfDate.Substring(12, 2))
                Dim d As New System.DateTime(year, month, day, hour, minute, second)
                PdfDateToString = d.ToLongDateString + " : " + d.ToLongTimeString
            Catch ex As Exception
                'Probably the date was not following the PDF date standard...
                'In that case we return the string
                PdfDateToString = pdfDate
            End Try
        Else
            'It is a date probably written in a human readable form
            PdfDateToString = pdfDate
        End If
    End Function
    Public Function CreateRectangleObject(ByVal rectangle As PdfRectangle) As clsPdfBaseObject
        CreateRectangleObject = CreateRectangleObject(rectangle.llx, rectangle.lly, rectangle.urx, rectangle.ury)
    End Function
    Public Function CreateRectangleObject(ByVal llx As Double, ByVal lly As Double, ByVal urx As Double, ByVal ury As Double) As clsPdfBaseObject
        'A rectangle object is an array object containing four numeric object
        Dim numericObject As clsPdfBaseObject
        Dim objectList As New List(Of clsPdfBaseObject)
        numericObject = CreateNumericObject(llx)
        objectList.Add(numericObject)
        numericObject = CreateNumericObject(lly)
        objectList.Add(numericObject)
        numericObject = CreateNumericObject(urx)
        objectList.Add(numericObject)
        numericObject = CreateNumericObject(ury)
        objectList.Add(numericObject)
        CreateRectangleObject = CreateArrayObject(objectList)
    End Function
    Public Function CreateArrayObject(ByVal objectList As List(Of clsPdfBaseObject)) As clsPdfBaseObject
        CreateArrayObject = New clsPdfBaseObject(PdfObjectTypes.ArrayObject, PdfObjectSubTypes.NoSubType)
        Dim obj As clsPdfBaseObject
        For Each obj In objectList
            CreateArrayObject.AddValue(obj)
            CreateArrayObject.AddValue(" "c)
        Next
    End Function
    Public Function CreateStreamObject() As clsPdfBaseObject
        CreateStreamObject = New clsPdfBaseObject(PdfObjectTypes.StreamObject, PdfObjectSubTypes.NoSubType)
    End Function
    Public Function CreateNameObject(ByVal s As String) As clsPdfBaseObject
        CreateNameObject = New clsPdfBaseObject(PdfObjectTypes.NameObject, PdfObjectSubTypes.NoSubType)
        CreateNameObject.SetString = s
    End Function
    Public Function CreateLiteralStringObject(ByVal s As String) As clsPdfBaseObject
        CreateLiteralStringObject = New clsPdfBaseObject(PdfObjectTypes.StringObject, PdfObjectSubTypes.LiteralStringObject)
        CreateLiteralStringObject.SetString = s
    End Function
    Public Function CreateNumericObject(ByVal number As Double) As clsPdfBaseObject
        CreateNumericObject = New clsPdfBaseObject(PdfObjectTypes.NumericObject, PdfObjectSubTypes.NoSubType)
        CreateNumericObject.AddValue(number)
    End Function
    Public Function CreateNumericObject(ByVal number As Long) As clsPdfBaseObject
        CreateNumericObject = New clsPdfBaseObject(PdfObjectTypes.NumericObject, PdfObjectSubTypes.NoSubType)
        CreateNumericObject.AddValue(number)
    End Function
    Public Function CreateIndirectReferenceObject(ByVal objectNumber As Long, ByVal generationNumber As Long) As clsPdfBaseObject
        CreateIndirectReferenceObject = New clsPdfBaseObject(PdfObjectTypes.IndirectReferenceObject, PdfObjectSubTypes.NoSubType)
        CreateIndirectReferenceObject.AddValue(objectNumber)
        CreateIndirectReferenceObject.ObjectNumber = objectNumber
        CreateIndirectReferenceObject.GenerationNumber = generationNumber
    End Function
    Public Function CreateIndirectObject(ByVal objectNumber As Long, ByVal generationNumber As Long) As clsPdfBaseObject
        CreateIndirectObject = New clsPdfBaseObject(PdfObjectTypes.IndirectObject, PdfObjectSubTypes.NoSubType)
        CreateIndirectObject.ObjectNumber = objectNumber
        CreateIndirectObject.GenerationNumber = generationNumber
    End Function
    Public Function CreateRectangle(ByVal llx As Double, ByVal lly As Double, ByVal urx As Double, ByVal ury As Double) As PdfRectangle
        CreateRectangle = New PdfRectangle
        CreateRectangle.llx = llx
        CreateRectangle.lly = lly
        CreateRectangle.urx = urx
        CreateRectangle.ury = ury
    End Function
    Public Function CreateDictionnaryObject(ByVal dictionnary As clsPdfDictionnary) As clsPdfBaseObject
        CreateDictionnaryObject = New clsPdfBaseObject(PdfObjectTypes.DictionaryObject, PdfObjectSubTypes.NoSubType)
        CreateDictionnaryObject.AddValue(dictionnary)
    End Function
    Public Function CreateBooleanObject(value As Boolean) As clsPdfBaseObject
        CreateBooleanObject = New clsPdfBaseObject(PdfObjectTypes.BooleanObject, PdfObjectSubTypes.NoSubType)
        If value Then
            CreateBooleanObject.SubType = PdfObjectSubTypes.TrueObject
            CreateBooleanObject.AddValue(PDF_TRUE)
        Else
            CreateBooleanObject.SubType = PdfObjectSubTypes.FalseObject
            CreateBooleanObject.AddValue(PDF_FALSE)
        End If
    End Function
End Module
